<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.tools.*"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>
<%@ page import="net.sf.json.*"%>
<%@ page import="java.io.*"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	UserBean user = (UserBean)request.getSession().getAttribute("userBean");

	String _db = user.getDbType();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/ligerui-icons.css" />
		<link type="text/css" rel="stylesheet" href="resource/css/style.css" />

		<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
		<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
		<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script type="text/javascript" src="js/validata.js"></script>

		<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerForm.js"></script>
		
		<script type="text/javascript">
			var _db = '<%=_db%>';
			
			_jsonObj.classname = "com.wxtf.platform.plugins.permissions.PermissionsAction";
			_jsonObj.methodname = "queryPage";
			_jsonObj.extenddata = '[{"id":0}]';
			
			var win;
			var manager;
	        $(function(){
				manager = $("#user-grid").ligerGrid({
					url: _url, parms: _jsonObj,enabledSort:false,
	                usePager:false, rownumbers:true, alternatingRow: true,
	                width: '100%', height: '100%',
	                tree: { columnId: 'text1', isExtend: false },
	                columns: [
	                    { display: 'id', name: 'id', hide: true,width:1 },
	                    { display: 'pid', name: 'pparent_id',width:1, hide: true },
	                    { display: 'logic_code', name: 'logic_code',width:1, hide: true },
	                    { display: '菜单名称', name: 'text', id:'text1', width: 300, align: 'left' },
	                	{ display: '菜单图标', name: 'icons', width: 60, align: 'center',
	                		render:function(record, index, value, oth){
	                			if(value == ''){
	                				return '';
	                			}
				        		return '<img src="ligerUI/skins/icons/' + value + '.gif" style="padding-top: 3px;"/>';
				        	}
	        			},
	                	{ display: '菜单url', name: 'action', width: 1, hide:true, align: 'left' },
	                	{ display: '菜单类型', name: 'types', width: 1, hide:true },
	                	//{ display: '快捷菜单', name: 'isQuick', width: 80, align: 'center',render:function(record, index, value, oth){ return value=='Y'? '是':'否'; } },
	                	{ display: '快捷菜单', name: 'isQuick', width: 1, hide:true },
	                	{ display: '快捷图标', name: 'quickIcon', width: 60, align: 'center',
	                		render:function(record, index, value, oth){
	                			if(value == ''){
	                				return '';
	                			}
				        		return '<img width="16" height="16" src="' + value + '" style="padding-top: 3px;" />';
				        	}
			        	},
	                	{ display: '排序号', name: 'showorder', width: 60, type:'int', align: 'center' },
	                	{ display: '新建',name:'add', width:60,render:function(record, index, value, oth){
	                			var str = '\'' + record.id + '\',\'' + record.text + '\',\'' + record.orderNum + '\''
	                			return '<a href="javascript:add(' + str + ')" class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/add.gif"/></a>';
	                		}
	                	},
	                	{ display: '编辑', name:'edit',width:60,render:function(record, index, value, oth){
	                			if(0 == record.id){
		                			return '';
	                			}
	                			var prow = manager.getParent(index);
	                			var str = '\'' + record.pparent_id + '\',\''
	                					+ prow.text + '\',\''
	                					+ record.id + '\',\''
	                					+ record.text + '\',\''
	                					+ record.action + '\',\''
	                					+ record.icons + '\',\''
	                					+ record.showorder + '\',\''
	                					+ record.isQuick + '\',\''
	                					+ record.quickIcon + '\',\''
	                					+ record.types + '\'';
	                			return '<a href="javascript:edit(' + str + ')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/edit.gif"/></div>';
	                		}
	                	},
	                	{ display: '删除', name:'delete',width:60,render:function(record, index, value, oth){
	                			if(0 == record.id){
		                			return '';
	                			}
	                			return '<a href="javascript:delData(\'' + record.id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/delete.gif"/></div>';
	                		}
	                	}
	                ],
	                onClickRows: function (data, rowindex, rowobj){
	                	
                	}
	            });
	            
	            
	            
	        });
	        
	        /*
	         * Grid页面刷新
	         */
	        function reload(){
	        	_jsonObj.methodname = "queryPage";
				_jsonObj.extenddata = '[{"id":0}]';
				manager.loadData();
	        }
	        
	        /*
	         * Grid页面新建
	         */
			function add(id, text, orderNum){
				document.forms[0].reset();
				document.getElementById('permission_id').value = '';
				document.getElementById('pparent_id').value = id;
				document.getElementById('pname').value = text;
				document.getElementById('showorder').value = (parseInt(orderNum) + 1) * 10;
				initWindowByDiv('editDiv', '新建菜单', 440, 320, save, closeWin);
			}
			/*
	         * Grid页面编辑
	         */
			function edit(pparent_id,text1,id,text,action,icons,showorder, isQuick,quickIcon,types){
				document.forms[0].reset();
				
				document.getElementById('pparent_id').value = pparent_id;
				document.getElementById('pname').value = text1;
				document.getElementById('permission_id').value = id;
				document.getElementById('name').value = text;
				document.getElementById('url').value = action;
				document.getElementById('icons').value = icons;
				document.getElementById('showorder').value = showorder;
				document.getElementById('quickIcon').value = quickIcon;
				document.getElementById('types').value = types;
				
				if(isQuick == 'Y'){
					document.getElementById('rbtnl_0').checked = "checked";
				}else{
					document.getElementById('rbtnl_1').checked = "checked";
				}
				initWindowByDiv('editDiv', '编辑菜单', 440, 320, save, closeWin);
			}
			/*
	         * Grid页面删除
	         */
			function delData(menuId){
				$.ligerDialog.confirm('确定要删除选中数据（包括其子节点）吗？\n删除后不可恢复！', function (ml) { 			
					if(ml){
						_jsonObj.methodname = "delete";
						_jsonObj.logicdata = "[{\"ids\":\"" + menuId + "\"}]";
						callserver(_jsonObj, function(data, satus){
							succMsgTip(data.resultrecord);
							reload();
						},function(data, satus){
							warningTip(data.resultrecord);
						});
					}
				});
			}
			
			/*
	         * Grid页面编辑
	         */
			function save(item, dialog) {
				var id = $('#permission_id').val();
				var pparent_id = $('#pparent_id').val();
				var text = $('#name').val();
				var action = $('#url').val();
				var showorder = $('#showorder').val();
				var icons = $('#icons').val();
				var isQuick = $('input:radio[name="isQuick"]:checked').val(); 
				var quickIcon = $('#quickIcon').val(); 
				var types = document.getElementById('types').value; 
				
				if(fucCheckLength(text.trim()) <= 0 || fucCheckLength(text.trim()) > 50){
					warningTip("菜单名称：不能为空且不能超过50字符");
					return;
				}
				if(fucCheckLength(action.trim()) > 150){
					warningTip("菜单路径：不能超过150字符");
					return;
				}
				if(!checkIsNum(showorder.trim())){
					warningTip("排序号：必须为整数");
					return;
				}
				if(Math.abs(parseInt(showorder)) > 1000000){
					warningTip("排序号：不能超过1000000");
					return;
				}
				
				if(id == ''){
					_jsonObj.methodname = "save";
				}else{
					_jsonObj.methodname = "update";
				}
				_jsonObj.logicdata = '[{"id":"'+id+'", "pparent_id":"'+pparent_id+'", "text":"'+text
					+'", "logic_code":"", "action":"'+action+'", "showorder":"'+showorder+'","icons":"'+icons
					+'","isQuick":"'+isQuick+'","quickIcon":"'+quickIcon+'","types":"'+types+'"}]';
				callserver(_jsonObj, function(data, satus){
					dialog.hide();
					succMsgTip(data.resultrecord);
					reload();
				},function(data, satus){
					warningTip(data.resultrecord);
				});
			}
			
			//图标弹出框
			var win1;
			function showIconWin(){
				if(win1){
					win1.show();
				} else {
					win1 = initWindowByDiv2('iconDiv', '选择图标', 300, 220, closeWin);
				}
			}
			function selectIcon(name){
				document.getElementById('icons').value = name;
				win1.hide();
			}
			function mouseover(ele){
				ele.style.backgroundColor="#FFB584";
			}
			function mouseout(ele){
				ele.style.backgroundColor="#FFFFFF";
			}
			//图标弹出框2
			function clearIcon(flag){
				if('N' == flag){
					document.getElementById('quickIcon').value = '';
				}
			}
			var win2;
			function showIconWin2(){
				var isQuick = $('input:radio[name="isQuick"]:checked').val();
				if(isQuick == 'N'){
					warningTip("当菜单为快捷菜单时，快捷图标才可用");
					return;
				}
				if(win2){
					win2.show();
				} else {
					win2 = initWindowByDiv2('iconDiv2', '选择图标', 360, 240, closeWin);
				}
			}
			function selectIcon2(name){
				document.getElementById('quickIcon').value = name;
				win2.hide();
			}
			
		</script>
	</head>

	<body>
		<div id="user-grid"></div>
		
		<div id="editDiv" style="display: none;">
			<form id="mform" name="mform">
				<input type="hidden" id="permission_id" name="permission_id" value="" />
				<input type="hidden" id="pparent_id" name="pparent_id" value="" />
				<table class="form_table">
					<tr>
						<td class="form_label">父菜单名称：</td>
						<td>
							<input type="text" id="pname" name="pname" class="textbg1" style="width: 240px;" readonly="readonly" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
						</td>
						<td></td>
					</tr>
					<tr>
						<td class="form_label">名称：</td>
						<td>
							<input id="name" name="name" class="textbg1" style="width: 240px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
						</td>
						<td align="left" style="color: red; ">*</td>
					</tr>
					<tr>
						<td class="form_label">URL：</td>
						<td>
							<input id="url" name="url" class="textbg1" style="width: 240px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
						</td>
						<td></td>
					</tr>
					<tr>
		                <td class="form_label">图标：</td>
		                <td>
							<input id="icons" name="icons" class="textbg1" style="width: 240px;" readonly="readonly" onclick="showIconWin();" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
		                </td>
		            </tr>
					<tr>
						<td class="form_label">菜单类型：</td>
						<td>
							<select id="types" name="types" style="width: 242px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'">
								<option value="iframe" selected="selected">iframe</option>
								<option value="newPage">newPage</option>
							</select>
						</td>
						<td></td>
					</tr>
					<tr>
						<td class="form_label">排序号：</td>
						<td>
							<input id="showorder" name="showorder" class="textbg1" style="width: 240px;" value="0" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
						</td>
						<td></td>
					</tr>
					<tr>
						<td class="form_label" valign="top">
							快捷菜单：
						</td>
						<td align="left">
							<input style="margin-top: 2px;" id="rbtnl_0" type="radio" name="isQuick" onclick="clearIcon('Y');" value="Y" checked="checked" />是
							<input style="margin-top: 2px;" id="rbtnl_1" type="radio" name="isQuick" onclick="clearIcon('N');" value="N" />否
						</td>
						<td></td>
					</tr>
					<tr>
						<td class="form_label">快捷图标：</td>
						<td>
							<input id="quickIcon" name="quickIcon" class="textbg1" value="" style="width: 240px;" readonly="readonly" onclick="showIconWin2();" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
						</td>
						<td></td>
					</tr>
				</table>
			</form>
		</div>
		
		<div id="iconDiv" style="display: none;">
			<table style=" height: 150px;">
				<%
				String filePath = request.getSession().getServletContext().getRealPath("/");
				filePath += "/ligerUI/skins/ligerui-icons.css";
				String str = Common.splitCss(filePath);
				
				JSONArray array = JSONArray.fromObject(str);
				int col = 10;
				String html = "";
				int colnum = (int)Math.ceil(array.size() * 1.0 / col);
				for(int i=0; i<colnum; i++){//行
					html += "<tr>";
					for(int j=0; j<col; j++){//列
						int k = col * i + j;
						if(k >= array.size()){
							continue;
						}
						JSONObject jsonObject = JSONObject.fromObject(array.getString(k));
						String key = jsonObject.getString("key");
						String icon = jsonObject.getString("icon");
						html += "<td align=\"center\" width=\"30\" height=\"30\" onmouseover=\"mouseover(this);\" onmouseout=\"mouseout(this);\" onclick=\"selectIcon('" + key + "');\"><img src=\"ligerUI/skins/" + icon + "\"/></td>";
					}
					html += "</tr>";
				}
				out.print(html);
				%>
			</table>
		</div>
		
		<div id="iconDiv2" style="display: none;">
			<table style=" height: 150px;">
				<%
				String dir = request.getSession().getServletContext().getRealPath("/resource/images/quickIcons/");
				int cols = 10;
				String htmls = "";
				File file = new File(dir);
				File[] images = file.listFiles();
				int colnums = (int)Math.ceil(images.length * 1.0 / col);

				for(int i=0; i<colnums; i++){//行
					htmls += "<tr>";
					for(int j=0; j<cols; j++){//列
						int k = cols * i + j;
						if(k >= images.length){
							continue;
						}
						String path1 = "resource/images/quickIcons/" + images[k].getName();
						htmls += "<td align=\"center\" width=\"40\" height=\"35\" onmouseover=\"mouseover(this);\" onmouseout=\"mouseout(this);\" onclick=\"selectIcon2('" + path1 + "');\"><img src=\"" + path1 + "\"/></td>";
					}
					htmls += "</tr>";
				}
				out.print(htmls);
				
				%>
			</table>
		</div>
	</body>
</html>


